package org.millioncall.yueyoga.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.util.WebAppUtils;
import org.millioncall.yueyoga.admin.model.Area;
import org.millioncall.yueyoga.admin.service.AreaService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

//--created by lisj on 2014.03.06
@Controller
public class AreaAction extends BaseAction {
	private AreaService areaService;

	public AreaService getareaService() {
		return areaService;
	}

	public void setareaService(AreaService areaService) {
		this.areaService = areaService;
	}

	@RequestMapping(value = "/findAreaList.jhtml")
	public String SearchArea(ModelMap context,String name,String provinceId,String cityId,String areaId, HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, context);
		int pageNo = 1;
		int pageSize = 10;

		String strStart = request.getParameter("pageNo");// 获得页码数
		if (StringUtils.isNoneBlank(strStart)) {
			pageNo = Integer.parseInt(strStart);
		}
		String strLimit = request.getParameter("limit");
		if (strLimit != null) {
			pageSize = Integer.parseInt(strLimit);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		
		map.put("level", 1);
		List<Area> areas = areaService.getAreaInPage(map);
		context.put("provinces", areas);
		map.clear();
		
		if(StringUtils.isNoneBlank(name)){
			map.put("name","%"+name+"%");
			context.put("name", name);
		}
		if(StringUtils.isNoneBlank(provinceId)){
			map.put("level", 2);
			map.put("parentId",provinceId);
			areas = areaService.getAreaInPage(map);
			context.put("citys", areas);
			context.put("provinceId", provinceId);
		}
		if(StringUtils.isNoneBlank(cityId)){
			map.put("level", 3);
			map.put("parentId",cityId);
			areas = areaService.getAreaInPage(map);
			context.put("areas", areas);
			context.put("cityId", cityId);
		}
		if(StringUtils.isNoneBlank(areaId)){
			map.put("level", 4);
			map.put("parentId",areaId);
			context.put("areaId", areaId);
		}
		PaginatedList<Area> p = areaService.getAreaInPage((pageNo - 1) * 10, pageSize, map);
		int totalspeakers = p.getPager().getCountOfTotalResults().intValue();
		context.put("areaList", p);
		context.put("totalspeakers", totalspeakers);
		
		return "area_search";
	}
	
	@RequestMapping(value = "/toAddArea.jhtml")
	public String toAddArea(ModelMap context,HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, context);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("level", 1);
		List<Area> areas = areaService.getAreaInPage(map);
		context.put("provinces", areas);
		return "area_add";
	}
	@RequestMapping(value = "/addArea.jhtml")
	public String addArea(ModelMap context,HttpServletRequest request, HttpServletResponse response) {
		super.initFrontHeader(request, context);
		Map<String, Object> map = new HashMap<String, Object>();
		String name =request.getParameter("name");
		map.put("name", name);
		map.put("enable",true);
		map.put("sort", 99);
		String provinceId =request.getParameter("provinceId");
		String cityId =request.getParameter("cityId");
		String areaId =request.getParameter("areaId");
		if(StringUtils.isBlank(provinceId)){
			map.put("level", 1);
			map.put("parentId", null);
		}
		if(StringUtils.isNoneBlank(provinceId)&&StringUtils.isBlank(cityId)){
			map.put("level", 2);
			map.put("parentId", provinceId);
		}
		if(StringUtils.isNoneBlank(cityId)&&StringUtils.isBlank(areaId)){
			map.put("level", 3);
			map.put("parentId", cityId);
		}
		if(StringUtils.isNoneBlank(areaId)){
			map.put("level", 4);
			map.put("parentId", areaId);
		}	
		if (areaService.savearea(map)) {
			context.put("msg","添加地区成功");
			return "redirect:findAreaList.jhtml";
		}else{
			context.put("msg","添加地区失败");
			return "area_add";
		}
		
	}
	
	// 删除会员等级
	@RequestMapping(value = "/deleteArea.jhtml")
	public void deleteArea(HttpServletRequest request,HttpServletResponse response) {
		String msg = null;
		String ids = request.getParameter("ids");
		if (StringUtils.isBlank(ids)) {
			msg = "{'success':false,'msg':'未提交待删除会员的编号'}";
		} else {
			String[] arrids = ids.split(",");
			Integer[] deids = new Integer[20];
			for (int i = 0; i < arrids.length; i++) {
				if (arrids[i] != null && !arrids[i].equals("")) {
					deids[i] = Integer.parseInt(arrids[i]);
				}
			}
			try {
				areaService.deleteareaByIds(deids);
				msg = "true";
			} catch (RuntimeException re) {
				msg = "false";
			}
		}
		WebAppUtils.ajaxResponse(response, msg);
	}

	@RequestMapping(value = "/subArea.jhtml")
	public void subArea(Integer level, Integer parentId,
			HttpServletRequest request, HttpServletResponse response) {
		String msg = "";
		Map<String, Object> map = new HashMap<String, Object>();
		level = level + 1;
		map.put("level", level);
		map.put("parentId", parentId);
		List<Area> Areas = areaService.getAreaInPage(map);
		if(Areas.size()>0) {
			StringBuffer sb = new StringBuffer("[");
			for (Area area : Areas) {
				sb.append("{");
				sb.append("'areaId':'" + area.getAreaId() + "',");
				sb.append("'name':'" + area.getName() + "',");
				
				sb.append("'level':'" + area.getLevel() + "',");
				sb.append("'parentId':'" + area.getParentId() + "'},");
			}
			String str = sb.toString();
			str = str.substring(0, str.length() - 1);
			str += "]";
			msg = "{'success':true, 'items':" + str + "}";
		} else {
			msg = "{'success':false, 'msg':'未找到信息！'}";
		}
		WebAppUtils.ajaxResponse(response, msg);
	}
	
}
